package xin.nick;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Nick
 * @see <a href="https://baomidou.com/pages/56bac0/">MyBatis-Plus代码生成器</a>
 * @see <a href="https://baomidou.com/pages/981406/">代码生成器配置新</a>
 * @since 2022/12/12
 */
public class CodeGenerator {

    /**
     * 数据库链接信息
     */
    private static String url = "jdbc:mysql://127.0.0.1:3306/project_db?useUnicode=true" +
            "&allowPublicKeyRetrieval=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
    private static String username = "root";
    private static String password = "root";

    /**
     * 生成路径配置
     */
    private static String projectPath = "/out/";
    private static String mappingPath = "/out/mapper/";

    /**
     * 模块信息
     */
    private static String packageNme = "xin.nick";
    private static String moduleName = "";

    public static void main(String[] args) {

        // 需要生成的表格,用 "," 分割
        String tableList = "t_role";

        // 输入需要过滤掉的表名前缀
        String tablePrefix = "t_";

        String author = "Nick";

        // 可以指定生成到项目目录下面
//        String finalProjectPath = System.getProperty("user.dir") + "/system";
//        String srcPath = "src/main/java/";
//        String resourcePath = "src/main/resources/";
//        projectPath = finalProjectPath + "/" + srcPath;
//        mappingPath = finalProjectPath + "/" + resourcePath + "mapper/";
//        System.out.println(finalProjectPath);
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    // 设置作者
                    builder.author(author)
                            .enableSwagger()
                            .disableOpenDir()
                            // 指定输出目录
                            .outputDir(projectPath);
                })
                .packageConfig(builder -> {
                    // 设置父包名
                    builder.parent(packageNme)
                            // 设置父包模块名
                            .moduleName(moduleName)
                            // serviceImpl 生成到 service
                            .serviceImpl("service")
                            // 设置mapperXml生成路径
                            .pathInfo(Collections.singletonMap(OutputFile.xml, mappingPath));
                })
                .strategyConfig(builder -> {
                    // 设置需要生成的表名
                    builder.addInclude(tableList)
                            // 设置过滤表前缀
                            .addTablePrefix(tablePrefix);
                })
                .templateConfig(builder -> {
                    // 模板配置禁止生成service
                    builder.disable(TemplateType.SERVICE);
                })
                .strategyConfig(builder -> {

                    // 设置Rest风格的Controller
                    builder.controllerBuilder().enableRestStyle();

                    // 配置Service
                    builder.serviceBuilder()
                            .formatServiceImplFileName("%sService")
                            ;

                    // mapper
                    builder.mapperBuilder().enableMapperAnnotation();

                    // 设置需要生成的Entity
                    builder.entityBuilder()
                            .enableLombok()
                            .enableRemoveIsPrefix()
                            .disableSerialVersionUID()
//                            .superClass(BaseEntity.class)
                            .logicDeleteColumnName("deleted")
                            .logicDeletePropertyName("deleted")
//                            .addIgnoreColumns("created_user_id", "create_time", "updated_user_id", "update_time", "deleted")
                            .enableTableFieldAnnotation().build();
                })


                // 配置生成其他的文件
                .injectionConfig(consumer -> {
                    Map<String, String> customFile = new HashMap<>();
                    // DTO,VO,QO
                    customFile.put("DTO", "/templates/dto.java.ftl");
                    customFile.put("create.DTO", "/templates/create.dto.java.ftl");
                    customFile.put("update.DTO", "/templates/update.dto.java.ftl");
                    customFile.put("VO", "/templates/vo.java.ftl");
                    customFile.put("Query", "/templates/query.java.ftl");
                    customFile.put("SQL", "/templates/authority.sql.ftl");
                    consumer.customFile(customFile);


                })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new EnhanceFreemarkerTemplateEngine())
                .execute();

    }

}
